package main

import (
	"fmt"
	"golang.org/x/net/websocket"
	"log"
	"mytest/mj-client/proto"
)

func main() {
	origin := "http://localhost/"
	url := "ws://localhost:8080/websocket?uid=1"
	conn, err := websocket.Dial(url, "", origin)
	if err != nil {
		log.Fatal(err)
	}
	go func() {
		for {
			var msg = make([]byte, 512)
			var n int
			if n, err = conn.Read(msg); err != nil {
				log.Fatal(err)
			}
			fmt.Printf("Received: %s.\n", msg[:n])
		}
	}()

	var i int32
	for i = 0; i < 10; i++ {
		msg := proto.Message{Code: 0, Msg: "success", Data: fmt.Sprintf("say %v ", i)}
		data, err := proto.Encode(msg, 2, i+1)
		if err != nil {
			fmt.Println("encode msg failed, err:", err)
			return
		}
		if _, err := conn.Write(data); err != nil {
			log.Fatal(err)
		}
	}
	select {}
}
